﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Business;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Entity.Vendas.Pedido.ClassesRelacionadas;
using Business.Forms;
using Business.Seguranca;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Forms.Vendas
{
    public partial class FormCondicaoPagamento : FormDefault, IForm
    {
        public FormCondicaoPagamento()
        {
            InitializeComponent();
            
        }

       
        public object Novo()
        {
            var c = new CondicaoPagamento();
            c.Prazos = new List<PrazoPagamento>();
            CurrentObject = c;
            ShowDialog();
            if (c.Id == 0)
            {
                return null;
            }
            return c;
        }

        public void Alterar(object entity)
        {
            CurrentObject = entity;
            ShowDialog();
        }



        private void cmdAddPrazo_Click(object sender, System.EventArgs e)
        {
            var p = new PrazoPagamento();
            prazosBindingSource.Add(p);
        }

        private void cmdExcluir_Click(object sender, System.EventArgs e)
        {
            prazosBindingSource.Remove(prazosBindingSource.Current);
        }

        public object Save()
        {
            if (!Validate().HasError)
            {
                CondicaoPagamentoRepository.Save((CondicaoPagamento) CurrentObject);
            }
            return CurrentObject;
        }

        public void Update(object entity)
        {
            Alterar(entity);
        }

        public bool Delete(object entity)
        {
            CurrentObject = entity;
            CondicaoPagamentoRepository.Delete((CondicaoPagamento) entity);
            return true;
        }


        public object CurrentObject
        {
            get
            {
                condicaoPagamentoBindingSource.EndEdit();
                return condicaoPagamentoBindingSource.DataSource;
            }
            set
            {
                condicaoPagamentoBindingSource.DataSource = value;
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return SegurancaUtils.GetPermissaoForm(Name);
            }
        }

        public EntityValidationResult Validate()
        {
            var cond = (CondicaoPagamento) CurrentObject;
            var result = DataValidation.ValidateEntity(cond);
            foreach (var prazo in cond.Prazos)
            {
                foreach (var error in DataValidation.ValidateEntity(prazo).ValidationErrors)
                {
                    result.ValidationErrors.Add(error);
                }
            }
            return result;
        }

        public void Reload()
        {
            
        }

        public void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public Object New()
        {
            return Novo();
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }
    }
}